$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;

    //点击返回按钮
    $(".head").on("click", ".goBack", function () {
        $.confirm("确定返回吗？", function () {
            localStorage.removeItem("work");
            window.location.href = "deliverBack.html";
        }, function () {
            $("#goods_id").focus();
        });
    });

    //搜索框 获取焦点
    $("#goods_id").focus();

    //点击 显示 隐藏
    $(".head .icon-xianshi").on("click", function () {
        $(this).hide();
        $(".head .icon-yincang").show();
        $(".details").show();
        //$alert("显示成功");
        $("#goods_id").val("").focus();
    });
    $(".head .icon-yincang").on("click", function () {
        $(this).hide();
        $(".head .icon-xianshi").show();
        $(".details").hide();
        //$alert("隐藏成功");
        $("#goods_id").val("").focus();
    });

    var work = JSON.parse(localStorage.getItem("work"));
    $(".details span.good_pm").html(work.productName);
    // $(".details span.good_js").html(work.productSpecification);
    $(".details span.good_js").html(work.planCount);
    $(".details span.good_zl").html(work.planWeight);
    // var locationHtml = '';
    // for (var i = 0; i < work.locationInventorys.length; i++){
    //     locationHtml += '<p>指引库位:<span class="good_location">'+
    //         work.locationInventorys[i]._id.location +'</span>件数:<span class="good_locationLength">'+
    //         work.locationInventorys[i].lengths +'</span>重量:<span class="good_locationWeight">'+
    //         work.locationInventorys[i].weights +'</span></p>'
    // }
    // $(".details").prepend(locationHtml);

    //根据扫描内容 得到 产品信息
    /*$("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            if (!val) {
                $alert("请扫描产成品标签！");
                setTimeout(function () {
                    pass = 1;
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            if ($(".content .list-block .goodsList_ul li[attr-data='" + val + "']").length != 0) {
                $alert("不可重复扫描");
                setTimeout(function () {
                    pass = 1;
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            $(this).val("");
            loading_show("获取中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + "/app/deliver/getProductwarehouse",
                data: {
                    _id: work.id,
                    batch: val
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        if ($(".content .list-block .goodsList_ul li[attr-data='" + data.batch + "']").length != 0) {
                            $alert("不可重复扫描");
                            setTimeout(function () {
                                $("#goods_id").val("").focus();
                            }, 1400);
                        } else {
                            if (/WN_CODE/.test(data.batch)) {
                                var data_html = '<li attr-data="' + data.batch + '"><div class="row no-gutter">' +
                                    '<div class="col-90">' +
                                    '<p><span>编号:</span><span class="sp goods_number">' +
                                    data.batch + '</span></p></div>' +
                                    '<div class="col-10"><span class="icon iconfont icon-msnui-jian pull-right"></span></div></div></li>';
                                $(".content .list-block .goodsList_ul").prepend(data_html);
                            } else {
                                var data_html = '<li attr-data="' + data.batch + '"><div class="row no-gutter">' +
                                    '<div class="col-90">' +
                                    '<p><span>编号:</span><span class="sp goods_number">' +
                                    data.batch + '</span></p><p><span>净重:</span><span class="sp goods_cpjz">' +
                                    data.availableQuantity + '</span> T</p></div>' +
                                    '<div class="col-10"><span class="icon iconfont icon-msnui-jian pull-right"></span></div></div></li>';
                                $(".content .list-block .goodsList_ul").prepend(data_html);
                            }
                            badge_text();
                            $alert(data.batch+"获取成功", 800);
                            setTimeout(function () {
                                $("#goods_id").val("").focus();
                            }, 1400);
                        }
                    } else {
                        setTimeout(function () {
                            $("#goods_id").val("").focus();
                        }, 1400);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    loading_hide();
                    console.log(error);
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });*/

    // li 中的 减号 点击 删除 本条
    $(".content").on("click", ".goodsList_ul li .col-10 .icon-msnui-jian", function () {
        var jianhao = $(this);
        $.confirm('确定删除此货品？', function () {
            jianhao.parent().parent().parent().remove();
            badge_text();
            $alert("删除成功");
            $("#goods_id").val("").focus();
        });
    });

    //点击 获取编号
    $(".foot").on("click", ".add_go", function () {
        if (pass == 0) {
            return
        }
        pass = 0;
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/deliver/getProductnumber',
            data: {
                _id: work.id,
            },
            dataType: 'json',
            json: 'callback',
            cache: false,
            success: function (res) {
                pass = 1;
                if (res.success) {
                    $(".mengceng").show();
                    $("div.div_pt").show();
                    $("div.div_pt input").val("");
                    $("div.div_pt .batch").val(res.result.number);
                    $("div.div_pt .weight").focus();
                } else {
                    $alert(res.message);
                }
            },
            error: function (error) {
                pass = 1;
                console.log(error);
                $alert(error.responseText);
            }
        });
    });
    //点击  取消
    $("div.div_pt .pt_quxiao").click(function () {
        $(".mengceng").hide();
        $("div.div_pt").hide();
        $("#goods_id").focus();
    });
    //点击  取消
    $("div.div_pt .pt_queding").click(function () {
        if (!$("div.div_pt .batch").val()) {
            return $alert("编号不可为空！");
        } else if (!$("div.div_pt .weight").val()) {
            return $alert("重量不可为空！");
        } else if (Number($("div.div_pt .weight").val()) <= 0) {
            return $alert("重量不可小于0！");
        } else {
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/deliver/checkProductnumber',
                data: {
                    _id: work.id,
                    batch: $("div.div_pt .batch").val(),
                    weight: $("div.div_pt .weight").val()
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                success: function (res) {
                    if (res.success) {
                        pass = 1;
                        var data = res.result;
                        var li_con = '<li attr-data="' + data.batch + '"><div class="row no-gutter"><div class="col-90">' +
                            '<p><span>编号：</span><span class="sp batch">' + data.batch + '</span></p>' +
                            '<p><span>物料编码：</span><span class="sp inv_Name">' + data.inv_Code + '</span></p>' +
                            '<p><span>物料名称：</span><span class="sp inv_Name">' + data.inv_Name + '</span></p>' +
                            '<p><span>物料规格：</span><span class="sp inv_Name">' + (data.inv_Spec || "") + '</span>' +
                            '<p><span>重量：</span><span class="sp weight">' + data.quantity + '</span>T</p>' +
                            '</div><div class="col-10"><span class="icon iconfont icon-msnui-jian"></span>';
                        if (data.batch.indexOf("TH") == 0) {
                            li_con += '<span class="icon iconfont icon-dayin" style="margin-top: 1.5rem"></span>';
                        }
                        li_con += '</div></div></li>';
                        $(".content .list-block .goodsList_ul").prepend(li_con);

                        badge_text();
                        $alert("添加成功", 800);
                        $(".mengceng").hide();
                        $("div.div_pt").hide();
                        $("#goods_id").val("").focus();
                        if (data.batch.indexOf("TH") == 0) {
                            takeupPrint("productback", data);
                        }
                    } else {
                        pass = 1;
                        console.log(res);
                        setTimeout(function () {
                            $("div.div_pt input.cpkw").val("").focus();
                        }, 1400);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    console.log(error);
                    setTimeout(function () {
                        $("div.div_pt input.cpkw").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });
    // li 中的 打印
    $(".content .goodsList_ul").on("click", '.icon-dayin', function () {
        var li = $(this).parent().parent().parent();
        var batch = li.find(".batch").text();
        var weight = li.find(".weight").text();
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/deliver/checkProductnumber',
            data: {
                _id: work.id,
                batch: batch,
                weight: weight
            },
            dataType: 'json',
            json: 'callback',
            cache: false,
            success: function (res) {
                if (res.success) {
                    pass = 1;
                    $alert(batch + "打印成功");
                    takeupPrint("productback", res.result);
                } else {
                    pass = 1;
                    console.log(res);
                    setTimeout(function () {
                        $("div.div_pt input.cpkw").val("").focus();
                    }, 1400);
                    $alert(res.message);
                }
            },
            error: function (error) {
                pass = 1;
                console.log(error);
                setTimeout(function () {
                    $("div.div_pt input.cpkw").val("").focus();
                }, 1400);
                $alert(error.responseText);
            }
        });
    });

    // 点击 添加发货 产品信息 传给后台
    $(document).on("click", ".foot .add_success", function () {
        if ($(".content .goodsList_ul li").length <= 0) {
            $("#goods_id").val("").focus();
            return $alert("请录入退货的产成品！");
        }
        $.confirm('共' + $(".content .goodsList_ul li").length + '件，确定退货吗？', function () {
            if (pass == 0) {
                return
            }
            pass = 0;
            var datas = [];
            for (var i = 0; i < $(".content .goodsList_ul li").length; i++) {
                datas.push({
                    batch: $(".content .goodsList_ul li").eq(i).find("span.batch").text(),
                    weight: $(".content .goodsList_ul li").eq(i).find("span.weight").text()
                });
            }
            loading_show("提交中...");
            //获取 货品列表中的信息
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "POST",
                url: url + "/app/deliver/saveSalebackorder",
                data: {
                    _id: work.id,
                    datas: JSON.stringify(datas)
                },
                timeout: 60000,
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        console.log("退货成功");
                        localStorage.removeItem("work");
                        window.location.href = "storekeeper.html";
                    } else {
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    if (error.status == 0) {
                        pass = 1;
                        loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        pass = 1;
                        loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        });
    });

    //点击 某一条 数据  改变 背景颜色
    $(document).on("click", ".content .goodsList_ul li", function () {
        $(this).css("background", "#cccccc").siblings("li").css("background", "#ffffff");
    });

    // goodsList中 badge 的值 为 列表中 li 的个数
    function badge_text() {
        var badge_len = $(".content .goodsList_ul li").length;
        //console.log(badge_len);
        var allWeight = 0;
        for (var i = 0; i < badge_len; i++) {
            var weight = $(".content .goodsList_ul li").eq(i).find(".row span.weight").html();
            allWeight = accAdd(allWeight, weight);
        }
        $(".details span.good_length").html(badge_len);
        $(".details span.good_weight").html(allWeight);
    }

    badge_text();

});